package com.kanshu.ksgb.fastread.doudou.module.makemoney.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseFragment;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.view.LoadingDialog;
import com.kanshu.ksgb.fastread.doudou.common.business.event.BindEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.event.LoginEvent;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.bean.EverydayBean;
import com.kanshu.ksgb.fastread.doudou.common.business.event.TaskEvent;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.presenter.MakeMoneyPresenter;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.retrofit.ReceiveParams;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.BindInfo;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UpdateInfoEvent;
import com.kanshu.ksgb.fastread.doudou.module.signin.event.SignInEvent;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by wcy on 18/5/28.
 */

public class EverydayFragment extends BaseFragment implements IGenrialMvpView<BaseResult<EverydayBean>> {

    @BindView(R.id.label1)
    TextView m30MinLabel;
    @BindView(R.id.label2)
    TextView m2CoinBean;
    @BindView(R.id.label3)
    SuperTextView m30MinProgress;
    @BindView(R.id.label5)
    TextView m90MinLabel;
    @BindView(R.id.label6)
    TextView m4CoinBean;
    @BindView(R.id.label7)
    SuperTextView m90MinProgress;
    @BindView(R.id.label9)
    TextView m150MinLabel;
    @BindView(R.id.label10)
    TextView m8CoinBean;
    @BindView(R.id.label11)
    SuperTextView m150MinProgress;
    @BindView(R.id.label13)
    TextView mBindPhone;
    @BindView(R.id.label14)
    TextView mOneYuan;
    @BindView(R.id.label15)
    SuperTextView mBindPhoneProgress;
    Unbinder unbinder;
    MakeMoneyPresenter mPresenter;
    EverydayBean mEverydayBean;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_everyday_make_money_layout, null);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        init();
        return view;
    }

    private void init() {
        mPresenter = new MakeMoneyPresenter(lifeCyclerSubject);
        mPresenter.setEverydayView(this);
        SwipeRefreshHelper.init(mSwipeRefresh, () -> mPresenter.getEverydayInfo());
        mPresenter.getEverydayInfo();
        final ReceiveParams params = new ReceiveParams();
        m30MinProgress.setOnClickListener(v -> {
            if (mEverydayBean != null && mEverydayBean.read_30 >= 30
                    && mEverydayBean.receive_time_30 <= 0) {
                showLoading("");
                params.receive_type = ReceiveParams.RECEIVE_TIME_30;
                mPresenter.receiveTask(params, new INetCommCallback<Boolean>() {
                    @Override
                    public void onResponse(Boolean aBoolean) {
                        dismissLoading();
                        m30MinProgress.setText("已领取");
                        setProgressViewColor(m30MinProgress,2);
                        EventBus.getDefault().post(new UpdateInfoEvent());
                    }

                    @Override
                    public void onError(int code, String errDesc) {
                        dismissLoading();
                        EventBus.getDefault().post(new UpdateInfoEvent());
                    }
                });
            }
        });
        m90MinProgress.setOnClickListener(v -> {
            if (mEverydayBean != null && mEverydayBean.read_90 >= 90
                    && mEverydayBean.receive_time_90 <= 0) {
                params.receive_type = ReceiveParams.RECEIVE_TIME_90;
                showLoading("");
                mPresenter.receiveTask(params, new INetCommCallback<Boolean>() {
                    @Override
                    public void onResponse(Boolean aBoolean) {
                        dismissLoading();
                        m90MinProgress.setText("已领取");
                        setProgressViewColor(m90MinProgress,2);
                        EventBus.getDefault().post(new UpdateInfoEvent());
                    }

                    @Override
                    public void onError(int code, String errDesc) {
                        EventBus.getDefault().post(new UpdateInfoEvent());
                        dismissLoading();
                    }
                });
            }

        });
        m150MinProgress.setOnClickListener(v -> {
            if (mEverydayBean != null && mEverydayBean.read_150 >= 150
                    && mEverydayBean.receive_time_150 <= 0) {
                showLoading("");
                params.receive_type = ReceiveParams.RECEIVE_TIME_150;
                mPresenter.receiveTask(params, new INetCommCallback<Boolean>() {
                    @Override
                    public void onResponse(Boolean aBoolean) {
                        dismissLoading();
                        m150MinProgress.setText("已领取");
                        setProgressViewColor(m150MinProgress,2);
                        EventBus.getDefault().post(new UpdateInfoEvent());
                    }

                    @Override
                    public void onError(int code, String errDesc) {
                        dismissLoading();
                        EventBus.getDefault().post(new UpdateInfoEvent());
                    }
                });
            }

        });
    }

    @Override
    protected void updateViews() {
        super.updateViews();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        mPresenter.detachView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleTaskEvent(TaskEvent event) {
        mPresenter.getEverydayInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleBindEvent(BindEvent event) {
        if (event.code == BindEvent.SUCCESS) {
            mPresenter.getEverydayInfo();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleBindInfo(BindInfo event) {
        if (event.result) {
            mPresenter.getEverydayInfo();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleSignInEvent(SignInEvent event) {
        if (event.status == SignInEvent.STATUS_SUCCESS) {
            mPresenter.getEverydayInfo();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleLoginEvent(LoginEvent event) {
        mPresenter.getEverydayInfo();
    }


    LoadingDialog loadingDialog;
    public void showLoading(String msg){
        if(loadingDialog == null){
            loadingDialog = new LoadingDialog(getContext(),msg);
        }
        if(!loadingDialog.isShowing()){
            loadingDialog.show();
        }
    }
    public void dismissLoading(){
        if(loadingDialog !=null && loadingDialog.isShowing()){
            loadingDialog.dismiss();
        }
    }

    @Override
    public void showContent(BaseResult<EverydayBean> result) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
        EverydayBean data = result.result.data;
        if (data == null) {
            return;
        }
        mEverydayBean = data;
/*        BookCityUtils.showHighLightNum(m2CoinBean, data.beans_30 + "金豆", data.beans_30);
        BookCityUtils.showHighLightNum(m4CoinBean, data.beans_90 + "金豆", data.beans_90);
        BookCityUtils.showHighLightNum(m8CoinBean, data.beans_150 + "金豆", data.beans_150);*/
        int bg1 = R.mipmap.ic_finish_task;
        int bg2 = R.mipmap.ic_wait_task;
        int color1 = Color.parseColor("#ff232b71");
        int color2 = Color.parseColor("#ffc66600");
        if (data.receive_time_30 > 0) {
            m30MinProgress.setText("已领取");
            setProgressViewColor(m30MinProgress,2);
        } else if (data.read_30 >= 30) {
            setProgressViewColor(m30MinProgress,1);
            m30MinProgress.setText("领取奖励");
        } else {
            setProgressViewColor(m30MinProgress,0);
            m30MinProgress.setText("待完成");
        }

        if (data.receive_time_90 > 0) {
            setProgressViewColor(m90MinProgress,2);
            m90MinProgress.setText("已领取");
        } else if (data.read_90 >= 90) {
            m90MinProgress.setText("领取奖励");
            setProgressViewColor(m90MinProgress,1);
        } else {
            m90MinProgress.setText("待完成");
            setProgressViewColor(m90MinProgress,0);
        }

        if (data.receive_time_150 > 0) {
            setProgressViewColor(m150MinProgress,2);
            m150MinProgress.setText("已领取");
        } else if (data.read_150 >= 150) {
            setProgressViewColor(m150MinProgress,1);
            m150MinProgress.setText("领取奖励");
        } else {
            setProgressViewColor(m150MinProgress,0);
            m150MinProgress.setText("待完成");
        }

        if (data.bind_phone > 0) {
            mBindPhoneProgress.setText("已绑定");
            setProgressViewColor(mBindPhoneProgress,2);
        } else {
            setProgressViewColor(mBindPhoneProgress,0);
            mBindPhoneProgress.setText("待完成");
        }

    }

    private void setProgressViewColor(SuperTextView textView,int progress){
        int color0 = mContext.getResources().getColor(R.color.make_money_text_color0);
        int color1 = mContext.getResources().getColor(R.color.make_money_text_color1);
        int color2 = mContext.getResources().getColor(R.color.make_money_text_color2);
        int white= mContext.getResources().getColor(R.color.white);
        switch (progress){
            case 0://待完成
                textView.setSolid(white);
                textView.setTextColor(color0);
                textView.setStrokeColor(color0);
                textView.setStrokeWidth(1);
                break;
            case 1://已完成，未领取
                textView.setStrokeColor(white);
                textView.setSolid(color0);
                textView.setTextColor(color1);
                textView.setStrokeWidth(0);
                break;
            case 2://已领取
                textView.setSolid(white);
                textView.setStrokeColor(color2);
                textView.setTextColor(color2);
                textView.setStrokeWidth(1);
                break;
        }

    }

    @Override
    public void showError(int errCode, String errDesc) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
    }
}
